<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit" :showHistoryBtn="true">
		<om-autoform ref="autoform" :checkedit="checkedit" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
		<om-sub :title="ms_title_title0" :subTitle="ms_subtitle_title0">
			<uni-list>
				<view v-for="(item, index) in workList_title0" :key="index">
					<uni-list-item :avatar="index + 1" :showArrow="true" @click="openWork0(index)">
						<view>{{ item.businessFormName }}</view>
						<view class="text-gray text-sm padding-bottom-xs padding-top-xs">{{ item.frequencyName }}</view>
						<view class="text-gray text-sm">{{ item.dateRemark }}</view>
					</uni-list-item>
				</view>
			</uni-list>
		</om-sub>
		<om-sub :title="ms_title_title1" :subTitle="ms_subtitle_title1">
			<uni-list>
				<view v-for="(item, index) in workList_title1" :key="index">
					<uni-list-item :avatar="index + 1" :showArrow="true" @click="openWork1(index)" :showClear="true" @iconClear="delSub1(index)">
						<view>{{ item.businessFormName }}</view>
						<view class="text-gray text-sm padding-bottom-xs padding-top-xs">{{ item.frequencyName }}</view>
						<view class="text-gray text-sm">{{ item.dateRemark }}</view>
					</uni-list-item>
				</view>
			</uni-list>
			<view v-if="!checkedit" class="tui-flex align-center justify-center padding-top padding-bottom-sm">
				<text class="lg text-green cuIcon-add" @click="openWork1()">{{ '增加' + ms_title_title1 }}</text>
			</view>
		</om-sub>
		<om-sub :title="ms_title_title2" :subTitle="ms_subtitle_title2">
			<uni-list>
				<view v-for="(item, index) in workList_title2" :key="index">
					<uni-list-item :avatar="index + 1" :showArrow="true" @click="openWork2(index)" :showClear="true" @iconClear="delSub2(index)">
						<view>{{ item.dutyRemark }}</view>
						<view class="text-gray text-sm padding-bottom-xs padding-top-xs">{{ item.frequencyName }}</view>
						<view class="text-gray text-sm">{{ item.dateRemark }}</view>
					</uni-list-item>
				</view>
			</uni-list>
			<view v-if="!checkedit" class="tui-flex align-center justify-center padding-top padding-bottom-sm">
				<text class="lg text-green cuIcon-add" @click="openWork2()">{{ '增加' + replaceShowText(ms_title_title2) }}</text>
			</view>
		</om-sub>
		<om-white-space bgColor="#eee"></om-white-space>
	</om-addpage>
</template>

<script>
import { getUrl, confirmModal, ptFloat, minusF } from '@/static/js/common.js';
import { formValidation, tranferToEditors, changeEditor, configSelectData, hasValue, formShowLoading, getFormFields } from '@/static/js/formUtil.js';
import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return {
			checkedit: false,
			menuName: '岗位职责调整确认表',
			id: '',
			busCode: 'MAN0303',
			formdata: {},
			mainConfigData: [],
			subConfigData: [],
			initValue: {},
			firstFlag: false,
			userPostCacheKey: 'adm-sta-prm-postDescription-userPost-cache',
			workConfigDataCacheKey: 'adm-sta-prm-postDescription-work-configdata-cache',
			workCacheKey_title0: 'adm-sta-prm-postDescription-work-title0-cache',
			workList_title0: [],
			ms_title_title0: '明细',
			ms_subtitle_title0: '',
			workCacheKey_title1: 'adm-sta-prm-postDescription-work-title1-cache',
			workList_title1: [],
			ms_title_title1: '明细',
			ms_subtitle_title1: '',
			workCacheKey_title2: 'adm-sta-prm-postDescription-work-title2-cache',
			workList_title2: [],
			ms_title_title2: '明细',
			ms_subtitle_title2: '',
			frequencyIdList: []
		};
	},
	computed: {
		...mapState(useLoginUserStore, ['userName', 'userId'])
	},
	methods: {
		replaceShowText(str) {
			 str = str.replace("(","")
			 str = str.replace(")","")
			 return str
		},
		
		onSelectZpos(item) {
			let _that = this;
			//清空数据
			_that.clearUserPost();
			//加载岗位
			_that.$refs.autoform.changeEditor('postTitle', 'userPostId', {
				link: '/pages/public/search/search',
				linkParam: {
					other: JSON.stringify({
						zposId: item.id
					}),
					namefield: 'userPosName',
					// notefield:"",
					// extrafield:"",
					searchfield: 'q',
					initload: true,
					url: 'postDescription/findPostList',
					cachename: _that.userPostCacheKey,
					placeholder: '请输入姓名或岗位'
				}
			});
		},

		clearZpos() {
			let _that = this;
			_that.$refs.autoform.clear('topinfo', 'zposId');
			_that.clearUserPost();
		},

		verifyUserPost() {
			let _that = this;
			if (_that.$refs.autoform.formdata.zposId) {
				return true;
			} else {
				return '请先选择制表岗位!';
			}
		},

		loadSubData(postId) {
			let _that = this;
			var formdata = _that.$refs.autoform.formdata;
			_that.om.request('postDescription/isAdjustPost', { postId: postId }).then(res => {
				if (res.succeed) {
					var value = res.value;
					if (value.flag) {
						confirmModal(_that, "提示信息",'该岗位职责正在调整中', function() {
							_that.clearUserPost();
						});
					} else {
						_that.firstFlag = value.firstFlag;
						_that.om.request('postDescription/getTitle0Data', { postId: postId }).then(res => {
							if (res.succeed) {
								_that.workList_title0 = res.value;
							} else {
								_that.om.toast(res.msg);
							}
						});
						_that.workList_title1 = value.objList;
						_that.workList_title2 = value.remarkList;
					}
				} else {
					_that.clearUserPost();
					_that.om.toast(res.msg);
				}
			});
		},

		clearUserPost() {
			let _that = this;
			_that.$refs.autoform.clear('postTitle', 'userPostId');
			_that.$refs.autoform.formdata.userPostId = '';
			_that.$refs.autoform.formdata.postId = '';
			_that.$refs.autoform.formdata.postName = '';
			_that.$refs.autoform.formdata.instId = '';
			_that.$refs.autoform.formdata.instName = '';
			_that.$refs.autoform.formdata.superInstId = '';
			_that.$refs.autoform.formdata.superInstName = '';
			_that.workList_title0 = [];
			_that.workList_title1 = [];
			_that.workList_title2 = [];
		},

		openWork0(index) {
			var checkRes = formValidation(this.$refs.autoform.configData, this.formdata);
			if (checkRes) {
				this.om.toast(checkRes);
			} else {
				uni.setStorage({
					key: this.workConfigDataCacheKey,
					data: this.subConfigData.title0
				});
				uni.setStorage({
					key: this.workCacheKey_title0,
					data: this.workList_title0
				});
				var param = {
					configDataCacheKey: this.workConfigDataCacheKey,
					cachename: this.workCacheKey_title0,
					index: index,
					checkedit: this.checkedit,
					frequencyIdList: this.frequencyIdList
				};
				var url = getUrl('data-info-title0', param);
				uni.navigateTo({
					url: url
				});
			}
		},

		openWork1(index) {
			var checkRes = formValidation(this.$refs.autoform.configData, this.formdata);
			if (checkRes) {
				this.om.toast(checkRes);
			} else {
				uni.setStorage({
					key: this.workConfigDataCacheKey,
					data: this.subConfigData.title1
				});
				uni.setStorage({
					key: this.workCacheKey_title1,
					data: this.workList_title1
				});
				var param = {
					configDataCacheKey: this.workConfigDataCacheKey,
					cachename: this.workCacheKey_title1,
					index: index,
					checkedit: this.checkedit,
					frequencyIdList: this.frequencyIdList,
					postId: this.formdata.postId
				};
				var url = getUrl('data-info-title1', param);
				uni.navigateTo({
					url: url
				});
			}
		},

		delSub1(index) {
			if (index != null) {
				let _that = this;
				if (_that.workList_title1[index].postDescriptionObjShowId) {
					_that.om.toast('只能删除本次新增的信息！');
				} else {
					this.workList_title1.splice(index, 1);
				}
			}
		},

		openWork2(index) {
			var checkRes = formValidation(this.$refs.autoform.configData, this.formdata);
			if (checkRes) {
				this.om.toast(checkRes);
			} else {
				uni.setStorage({
					key: this.workConfigDataCacheKey,
					data: this.subConfigData.title2
				});
				uni.setStorage({
					key: this.workCacheKey_title2,
					data: this.workList_title2
				});
				var param = {
					configDataCacheKey: this.workConfigDataCacheKey,
					cachename: this.workCacheKey_title2,
					index: index,
					checkedit: this.checkedit,
					frequencyIdList: this.frequencyIdList,
					postId: this.formdata.postId
				};
				var url = getUrl('data-info-title2', param);
				uni.navigateTo({
					url: url
				});
			}
		},

		delSub2(index) {
			if (index != null) {
				let _that = this;
				if (_that.workList_title2[index].postDescriptionRemarkShowId) {
					_that.om.toast('只能删除本次新增的信息！');
				} else {
					this.workList_title2.splice(index, 1);
				}
			}
		},
		formSubmit() {
			let _that = this;
			var checkRes = formValidation(_that.$refs.autoform.configData, _that.formdata);
			if (checkRes) {
				_that.om.toast(checkRes);
			} else {
				_that.formdata.defaultObjStr = JSON.stringify(_that.workList_title0);
				_that.formdata.objStr = JSON.stringify(_that.workList_title1);
				_that.formdata.remarkStr = JSON.stringify(_that.workList_title2);
				_that.formdata.powerRemarkStr = "[]";
				if (_that.checkedit) {
					// _that.om.formSave('postDescription/doReviewUpdate', _that.formdata, _that);
				} else {
					_that.formdata.firstFlag = _that.firstFlag;
					_that.om.formSave('postDescription/doAdd', _that.formdata, _that);
				}
			}
		},
		initLoad(fileId) {
			let _that = this;
			if (!this.busCode) {
				return;
			}
			//表单字段
			var pro1 = this.om.request('formFieldConfig/addPage', { code: this.busCode });
			//制表岗位
			var pro2 = this.om.request('wxPosBusiAuth/isnewList', { code: this.busCode }, 'post');
			//指定频次id集合
			var pro3 = this.om.request('postDescription/findFrequencyList', { parentCode: '37912' });
			var ajaxArr = [pro1, pro2, pro3];
			if (fileId) {
				//加载初始数据
				var pro3 = this.om.request('postDescription/getValue', { fileId: fileId });
				ajaxArr.push(pro3);
			}
			//多个AJAX全部完成时开始执行
			Promise.all(ajaxArr)
				.then(res => {
					if (res[0].succeed) {
						let data = res[0].value;
						var mainData = [
							{
								key: 'topinfo',
								editors: tranferToEditors(data.top)
							}
						];
						if (data.bodyTitle.length > 0) {
							for (var i = 0; i < data.bodyTitle.length; i++) {
								var datas = data['body' + i];
								if (data.bodyTitle[i].issub) {
									var key = data.bodyTitle[i].fieldId;
									var titleName = data.bodyTitle[i].itemName;
									if (key == 'title0') {
										_that.ms_title_title0 = titleName;
									} else if (key == 'title1') {
										_that.ms_title_title1 = titleName;
									} else if (key == 'title2') {
										_that.ms_title_title2 = titleName;
									}
									_that.subConfigData[key] = [
										{
											title: titleName,
											key: key,
											issub: true,
											editors: tranferToEditors(datas)
										}
									];
								} else {
									mainData.push({
										title: data.bodyTitle[i].itemName,
										key: data.bodyTitle[i].fieldId,
										editors: tranferToEditors(datas)
									});
								}
							}
						}
						//制表岗位
						changeEditor(mainData[0].editors, 'zposId', 'data', configSelectData(res[1], 'id', 'posname'));
						//指定频次集合
						// _that.frequencyIdList = res[2].value;
						let frequencyList = res[2];
						if (frequencyList) {
							for (var i in frequencyList) {
								let frequency = frequencyList[i];
								if (frequency && frequency.id) {
									_that.frequencyIdList.push(frequency.id);
								}
							}
						}
						//主表模板
						_that.$refs.autoform.configData = mainData;
						// if(res.length == 4){
						if (fileId) {
							var formData = res[3].value;
							var fieldArr = getFormFields(mainData);
							fieldArr.push('id');
							// fieldArr.push("dataStr")
							
							var defaultObjList = _that._.cloneDeep(formData.defaultObjList);
							_that.workList_title0 = defaultObjList;
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, 'defaultObjStr', JSON.stringify(defaultObjList));
							_that.$set(_that.formdata, 'defaultObjStr', JSON.stringify(defaultObjList));
							delete formData.defaultObjList;

							var objList = _that._.cloneDeep(formData.objList);
							_that.workList_title1 = objList;
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, 'objStr', JSON.stringify(objList));
							_that.$set(_that.formdata, 'objStr', JSON.stringify(objList));
							delete formData.objList;

							var remarkList = _that._.cloneDeep(formData.remarkList);
							_that.workList_title2 = remarkList;
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, 'remarkStr', JSON.stringify(remarkList));
							_that.$set(_that.formdata, 'remarkStr', JSON.stringify(remarkList));
							delete formData.remarkList;

							for (var i in formData) {
								if (fieldArr.includes(i)) {
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata, i, formData[i]);
									_that.$set(_that.formdata, i, formData[i]);
								}
							}
						}
					} else {
						_that.om.toast(res[0].msg);
					}
				})
				.catch(err => {
					console.log(err);
				});
		}
	},
	onLoad(options) {
		let _that = this;
		_that.menuName = options.title || '岗位职责调整确认表';
		uni.setNavigationBarTitle({
			title: _that.menuName
		});
		formShowLoading(_that);
		_that.busCode = options.code || 'MAN0303';
		_that.formdata.userId = this.userId;
		if (options.checkedit) {
			if (typeof options.checkedit == 'string') {
				if (options.checkedit == 'true') {
					_that.checkedit = true;
				}
			} else {
				_that.checkedit = options.checkedit;
			}
		}
		uni.removeStorage({
			key: _that.workCacheKey_title0
		});
		uni.removeStorage({
			key: _that.workCacheKey_title1
		});
		uni.removeStorage({
			key: _that.workCacheKey_title2
		});
		_that.initLoad(options.fileId);
	},
	onShow() {
		let _that = this;
		if (_that.$refs.autoform) {
			uni.getStorage({
				key: _that.userPostCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data;
						var formdata = _that.$refs.autoform.formdata;
						formdata.userPostId = d.id;
						formdata.postId = d.posId;
						formdata.postName = d.posName;
						formdata.instId = d.instId;
						formdata.instName = d.instName;
						formdata.superInstId = d.parentInstId;
						formdata.superInstName = d.parentInstName;
						_that.workList_title0 = [];
						_that.workList_title1 = [];
						_that.workList_title2 = [];
						_that.loadSubData(formdata.postId);
						uni.removeStorage({
							key: _that.userPostCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key: _that.workCacheKey_title0,
				success: function(res) {
					_that.workList_title0 = res.data;
					if (_that.workList_title0.length) {
						_that.$refs.autoform.formdata.defaultObjStr = JSON.stringify(_that.workList_title0);
					} else {
						_that.$refs.autoform.formdata.defaultObjStr = '';
					}
					uni.removeStorage({
						key: _that.workCacheKey_title0
					});
				}
			});
			uni.getStorage({
				key: _that.workCacheKey_title1,
				success: function(res) {
					_that.workList_title1 = res.data;
					if (_that.workList_title1.length) {
						_that.$refs.autoform.formdata.objStr = JSON.stringify(_that.workList_title1);
					} else {
						_that.$refs.autoform.formdata.objStr = '';
					}
					uni.removeStorage({
						key: _that.workCacheKey_title1
					});
				}
			});
			uni.getStorage({
				key: _that.workCacheKey_title2,
				success: function(res) {
					_that.workList_title2 = res.data;
					if (_that.workList_title2.length) {
						_that.$refs.autoform.formdata.remarkStr = JSON.stringify(_that.workList_title2);
					} else {
						_that.$refs.autoform.formdata.remarkStr = '';
					}
					uni.removeStorage({
						key: _that.workCacheKey_title2
					});
				}
			});
		}
	}
};
</script>
<style></style>
